//
//  ClinicalTrialGov+ClinicalTrialGov.m
//  ClinicalCollections
//
//  Created by janakiraman gopinath on 2/10/13.
//  Copyright (c) 2013 GOPI. All rights reserved.
//

#import "ClinicalTrialGov+ClinicalTrialGov.h"

@implementation ClinicalTrialGov (ClinicalTrialGov)

#define DETAILED_DESCRIPTION @"detailed_description.textblock"
#define FIRST_RECEIVED_DATE @"firstreceived_date.value"
#define COMPLETION_DATE @"completion_date.value"
#define FIRST_RECEIVED_RESULTS_DATE @"firstreceived_results_date.value"
#define START_DATE @"start_date.value"
#define END_DATE @"end_date.value"
#define NUMBER_OF_ARMS @"number_of_arms"
#define NUMBER_OF_GROUPS @"number_of_groups"
#define PHASE @"phase"
#define STUDY_DESIGN @"study_design"
#define STUDY_TYPE @"study_type"

+ (ClinicalTrialGov *) clinicalTrialGov:(Search *) search
                       resultDictionary:(NSDictionary *) results
                            withContext:(NSManagedObjectContext *) context
{
    ClinicalTrialGov *clinicalTrialGov = [NSEntityDescription insertNewObjectForEntityForName:@"ClinicalTrialGov"
                                                                       inManagedObjectContext:context];
    clinicalTrialGov.search = search;
    clinicalTrialGov.nctID = [results valueForKey:ID_KEY];
    clinicalTrialGov.briefTitle  = [results valueForKey:BRIEF_TITLE_KEY];
    clinicalTrialGov.status = [results valueForKey:OVERALL_STATUS];
    clinicalTrialGov.detailed_desc = [results valueForKeyPath:DETAILED_DESCRIPTION];
    clinicalTrialGov.first_received_date = [results valueForKeyPath:FIRST_RECEIVED_DATE];
    clinicalTrialGov.first_received_results_date = [results valueForKeyPath:FIRST_RECEIVED_RESULTS_DATE];
    clinicalTrialGov.completion_date = [results valueForKeyPath:COMPLETION_DATE];
    clinicalTrialGov.start_date = [results valueForKeyPath:START_DATE];
    clinicalTrialGov.end_date = [results valueForKeyPath:END_DATE];
    
    if (!([[results valueForKey:NUMBER_OF_ARMS] isEqual:[NSNull null]]))
         clinicalTrialGov.number_of_arms = [results valueForKey:NUMBER_OF_ARMS];
    
    if (!([[results valueForKey:NUMBER_OF_GROUPS] isEqual:[NSNull null]]))
        clinicalTrialGov.number_of_groups = [results valueForKey:NUMBER_OF_GROUPS];
    if (!([[results valueForKey:PHASE] isEqual:[NSNull null]]))
        clinicalTrialGov.phase = [results valueForKey:PHASE];
    if (!([[results valueForKey:STUDY_DESIGN] isEqual:[NSNull null]]))
        clinicalTrialGov.study_design = [results valueForKey:STUDY_DESIGN];
    if (!([[results valueForKey:STUDY_TYPE] isEqual:[NSNull null]]))
        clinicalTrialGov.study_type = [results valueForKey:STUDY_TYPE];

    return clinicalTrialGov;
}
+ (ClinicalTrialGov *) clinicalTrialGov:(NSString *) nctId
                                 search:(NSString *) searchString
                                context:(NSManagedObjectContext *) context
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"ClinicalTrialGov"];
    request.predicate = [NSPredicate predicateWithFormat:@"nctID= %@", nctId];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"nctID" ascending:YES];
    request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

    
    NSError *error = nil;
    NSArray *clinicalTrialGovs = [context executeFetchRequest:request error:&error];
    
   for (ClinicalTrialGov *ct in clinicalTrialGovs)
       if ([ct.search.searchString isEqualToString:searchString])
           return ct;

    return nil;
}
+ (Search *) SearchForClinicalTrialGov:(NSString *) nctId
                               context:(NSManagedObjectContext *) context
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"ClinicalTrialGov"];
    request.predicate = [NSPredicate predicateWithFormat:@"nctID= %@", nctId];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"nctID" ascending:YES];
    request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    ClinicalTrialGov *clinicalTrialGov;
    
    NSError *error = nil;
    NSArray *clinicalTrialGovs = [context executeFetchRequest:request error:&error];
    
    if (!clinicalTrialGovs) {
    } else if (![clinicalTrialGovs count]) {
        NSLog(@"No data found");
    } else {
        clinicalTrialGov = [clinicalTrialGovs lastObject];
    }
    return clinicalTrialGov.search;
}
@end
